/**
 * Created by cjd on 2016/6/7.
 */
angular.module("app").controller("org.list.Ctrl", ["$scope", "$http", "org", function ($scope, $http, org) {
    $scope.orgGridConfig = {
        bindingOptions: {
            selectedRowKeys: "selectedItems",
            dataSource: "orgGridConfig.dataSource"
        },
        twoWayBindingEnabled: false,
        searchPanel: {
            visible: true,
            width: 200,
            placeholder: "输入关键字进行查询"
        },
        paging: {
            pageSize: 15
        },
        groupPanel: {
            visible: true,
            emptyPanelText: "拖动列名进行分组和排序"
        },
        hoverStateEnabled: true,
        showBorders: true,
        showRowLines: true,
        rowAlternationEnabled: true,
        noDataText: "没有数据",
        columns: [{
            dataField: "orgName",
            alignment: "center",
            caption: "组织名称"
        }, {
            dataField: "onePass",
            alignment: "center",
            caption: "一号通"
        }, {
            dataField: "orgCode",
            alignment: "center",
            width: 80,
            caption: "组织编码"
        }, {
            dataField: "principal",
            alignment: "center",
            caption: "联系人"
        }, {
            dataField: "phone",
            alignment: "center",
            caption: "联系电话"
        }, {
            dataField: "status",
            alignment: "center",
            cellTemplate: "statusCellTemplate",
            width: 60,
            caption: "状态"
        }, {
            dataField: "operation",
            alignment: "center",
            cellTemplate: "operationCellTemplate",
            caption: "操作"
        }]
    };
    $scope.orgParent = $scope.$parent.treeViewDataSource[0].orgId;
    $scope.getList = function (orgId) {
        org.getList({}, {orgId: orgId}, function (data) {
            if (data.statusCode == 0) {
                $scope.orgGridConfig.dataSource = data.data.filter(function(item){
                    return item.orgId != item.orgParent
                });
            }
        });
    };
    $scope.updateOrgStatus = function (orgInfo) {
        org.updateStatus({}, {orgId: orgInfo.orgId}, function (data) {
            if (data.statusCode == 0) {
                if (orgInfo.status == 10) {
                    orgInfo.status = 20;
                }
                else {
                    orgInfo.status = 10;
                }
                alert("更新成功!")
            }
            else {
                alert(data.statusMessage)
            }
        });
    };
    $scope.treeViewConfig = angular.copy($scope.$parent.treeViewConfig);
    $scope.treeViewConfig.onItemClick = function (e) {
        $scope.getList(e.itemData.orgId);
        $scope.orgParent = e.itemData.orgId;
        $scope.orgInfo = {};
    };
    $scope.showOrgInfo = function (orgInfo) {
        $scope.orgInfo = orgInfo;
        $scope.type = "view";
        $scope.provinceChange("view");
        $scope.cityChange("view");
    };
    $scope.editOrgInfo = function (orgInfo) {
        $scope.orgInfo = orgInfo;
        $scope.type = "edit";
        $scope.provinceChange("view");
        $scope.cityChange("view");
    };
    $scope.saveOrg = function () {
        if($scope.isSocialCreditCode($scope.orgInfo.orgCode)){
            if(confirm("是否确定执行该操作?")){
                delete $scope.orgInfo.createTime;
                org.saveOrg({}, $scope.orgInfo, function (data) {
                    if (data.statusCode == 0) {
                        alert("操作成功!");
                        location.reload(true)
                    }
                });
            }
        }
        else{
            alert("组织编码有误,请重新输入!")
        }
    };
    $scope.isSocialCreditCode = function (code) {
        if (code.length != 18) {
            return false;
        }//必须是18位
        //登记管理部门代码1位
        var first = code.substring(0, 1);
        if (!/^[1,5,9,Y]{1}$/.test(first)) {
            return false;
        }//第一位数必须是以1，5，9或Y开头

        //机构类别代码1位
        var second = code.substring(1, 2);
        switch (first) {
            case "1" :
                if (!/^[1,2,3,9]$/.test(second)) {
                    return false;
                };
                break;//机构编制
            case "5" :
                if (!/^[1,2,3,9]$/.test(second)) {
                    return false;
                };
                break;//民政
            case "9" :
                if (!/^[1,2,3]$/.test(second)) {
                    return false;
                };
                break;//工商
            case "Y" :
                if (!/^[1]$/.test(second)) {
                    return false;
                };
                break;//其他
        }

        //登记管理机关行政区划码6位
        var xz = code.substring(2, 8);
        if (!/^[0-9]{6}$/.test(xz)) {
            return false;
        }

        //主体标识码（组织机构代码）9位
        var jg = code.substring(8, 17);
        if (!/^[0-9,A-H,J-N,P-R,T-U,W-Y]{9}$/.test(jg)) {
            return false;
        }//阿拉伯数字或者大写英语字母（不含I,O,Z,S,V）

        //第18位校验码
        var et = code.substring(17);

        /**计算第18位校证码*/
        //17位Ci的值
        var dmj = "0123456789ABCDEFGHJKLMNPQRTUWXY";
        var dmjv = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30);//字符集对应的值
        var Ci = new Array(17);
        for (var i = 0; i < 17; i++) {//17位
            var val = code.charAt(i);
            var indx = dmj.indexOf(val);
            Ci[i] = dmjv[indx];
        }
        //加权因子Wi
        var Wi = new Array(1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28);
        //Ci*Wi
        var CWi = new Array(17);
        var total = new Number(0);
        for (var i = 0; i < 17; i++) {
            var val = Ci[i] * Wi[i];
            CWi[i] = val;
            total += val;
        }
        var index = total % 31;
        var jm = "";
        if (index == 0) {
            jm = "0";
        } else {
            jm = dmj.charAt(31 - total % 31);
        }
        if (jm != et) {
            return false;
        }
        return true;
    };
    $scope.updateStatus = function (orgInfo) {
        if(confirm("是否确定执行该操作?")){
            $scope.updateOrgStatus(orgInfo);
        }
    };
    $scope.getList($scope.$parent.treeViewDataSource[0].orgId);
    $scope.createOrg = function () {
        $scope.type = "edit";
        $scope.orgInfo = {orgParent:$scope.orgParent,province:$scope.$parent.treeViewDataSource[0].province};
        $scope.provinceChange();
        $scope.cityChange();
    };
    $scope.provinceChange = function(type){
        $scope.currCityList = city.filter(function (item) {
            return item.ProID == $scope.orgInfo.province;
        });
        if(type != "view"){
            $scope.orgInfo.city = $scope.currCityList[0].CityID;
            $scope.cityChange();
        }
    };
    $scope.cityChange = function(type){
        $scope.currDistrictList = District.filter(function (item) {
            return item.CityID == $scope.orgInfo.city;
        });
        if(type != "view"){
            $scope.orgInfo.county = $scope.currDistrictList[0].Id;
        }
    }
}]);